package changceilingcolor003;

import processing.core.PApplet;
import processing.core.PImage;
import toxi.geom.Vec3D;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

import org.w3c.dom.*;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class Environment {

	ChangCeilingColor003 p5;

	String [] origins1;
	String [] origins2;
	String [] origins3;
	String [] boundries1;
	String [] boundries2;
	String [] boundries3;
	String [] boundries4;
	String [] boundries5;
	String [] distraction;

	Environment(ChangCeilingColor003 _p5) {		
		p5=_p5;
		Boundries();
		ReadAttractionState("data/"+p5.SiteFolder+"/AttractionLimit.xml");

	}
	//

	void run() 
	{
		//buildBox(p5.width, p5.height, p5.width/5);
		buildBox(p5.width, p5.height, 120);
		
		
		
		if(p5.DisplayPeopleTemperator)
		{
			for(int f=0;f<p5.m_num_of_floor;++f)
			{
				if(!p5.visible_floors.get(f))
					continue;
				PImage img = p5.createImage(p5.width, p5.height, p5.RGB);
				img.loadPixels();
				for (int i = 0; i < img.width; i++) 
				{
					for (int j = 0; j < img.height; j++) {
						img.set(i, j, p5.color(p5.PrevTemperatureGrid[i][j][f],
								p5.PrevTemperatureGrid[i][j][f],
								p5.PrevTemperatureGrid[i][j][f]));
					}
				}
				
				img.updatePixels();

				p5.noStroke();
				p5.beginShape();
				p5.texture(img);

				p5.textureMode(p5.NORMAL);
				p5.vertex(0, 0, f*p5.m_FloorHeight,0,0);
				p5.vertex(p5.width, 0, f*p5.m_FloorHeight,1,0);
				p5.vertex(p5.width, p5.height, f*p5.m_FloorHeight,1,1);
				p5.vertex(0, p5.height, f*p5.m_FloorHeight,0,1);
				p5.endShape();
			}
		}

//		for (int i=0;i<p5.alisOrigin1.size();i++) 
//		{
//			Vec3D v=p5.alisOrigin1.get(i).GetLocation();
//			p5.stroke(235);
//			p5.strokeWeight(3);
//			//p5.point(v.x, v.y, v.z);
//			p5.strokeWeight(1);
//			//p5.line(v.x, v.y+5, v.x, v.y-5);
//			p5.stroke(255,0,0,80);
//			p5.strokeWeight(1);
//			//p5.rect(v.x, v.y+15,5,15);
//			//p5.line(v.x, v.y+15, v.x+15, v.y+15);
//			p5.rect(v.x-7, v.y+5,15,15);
//
//		}
//
//		for (int i=0;i<p5.alisOrigin2.size();i++) 
//		{
//			Vec3D v=(Vec3D)p5.alisOrigin2.get(i).GetLocation();
//			p5.stroke(235);
//			p5.strokeWeight(3);
//			//p5.point(v.x, v.y, v.z);
//			p5.strokeWeight(1);
//			//p5.line(v.x+5, v.y, v.x-5, v.y);
//			p5.stroke(255,0,0,80);
//			p5.strokeWeight(1);
//			//p5.rect(v.x+15, v.y,15,5);
//			p5.rect(v.x+5, v.y-20,15,15);
//		}
//
//		for (int i=0;i<p5.alisOrigin3.size();i++) 
//		{
//			Vec3D v=(Vec3D)p5.alisOrigin3.get(i).GetLocation();
//			p5.stroke(235);
//			p5.strokeWeight(3);
//			//p5.point(v.x, v.y, v.z);
//			p5.strokeWeight(1);
//			//p5.line(v.x, v.y+5, v.x, v.y-5);
//			p5.stroke(255,0,0,80);
//			p5.strokeWeight(1);
//			//p5.rect(v.x, v.y-30,5,15);
//			p5.rect(v.x-7, v.y-20,15,15);
//		}

		//boundries
		if(p5.showBoundary)
		{
			for (int i=0;i<p5.alisBnd1.size();i++) {
				Vec3D v=(Vec3D)p5.alisBnd1.get(i);
				//Vec3D u=(Vec3D)p5.alisBnd1.get(i+1);
				//p5.stroke(255);
				p5.stroke(226,223,199,90);
				p5.strokeWeight(2);
				//p5.line(v.x, v.y, u.x, u.y);
				p5.point(v.x, v.y, v.z);
			}

			for (int i=0;i<p5.alisBnd2.size();i++) {
				Vec3D v=(Vec3D)p5.alisBnd2.get(i);
				//p5.stroke(255,90);
				p5.stroke(226,223,199,80);
				p5.strokeWeight(3);
				p5.point(v.x, v.y, v.z);
			}

			for (int i=0;i<p5.alisBnd3.size();i++) {
				Vec3D v=(Vec3D)p5.alisBnd3.get(i);
				//p5.stroke(100,20);
				//p5.stroke(226,223,199,30);
				p5.stroke(226,223,199,90);
				p5.strokeWeight(2);
				p5.point(v.x, v.y, v.z);
			}

			
			for (int i=0;i<p5.alisBnd4.size();i++) {
				Vec3D v=(Vec3D)p5.alisBnd4.get(i);
				p5.stroke(100,60);
				p5.strokeWeight(1);
				p5.point(v.x, v.y, v.z);
			}
			
			for (int i=0;i<p5.alisBnd5.size();i++) {
				Vec3D v=(Vec3D)p5.alisBnd5.get(i);
				p5.stroke(169,169,0,20);
				p5.strokeWeight(1);
				p5.point(v.x, v.y, v.z);
			}

		}
		//


	}

	void Boundries() {
		boundries1 =p5.loadStrings("data/"+p5.SiteFolder+"/BND1.txt"); 
		for (int i= 0; i <boundries1.length; i++) {  
			String [] fields = PApplet.split(boundries1[i], ',');
			float x = Float.valueOf(fields[0]);
			float y = Float.valueOf(fields[1]);
			float z = Float.valueOf(fields[2]);
			Vec3D v = new Vec3D (x, y, z);
			p5.alisBnd1.add(v);
		}

		boundries2 =p5.loadStrings("data/"+p5.SiteFolder+"/BND2.txt"); 
		for (int i= 0; i <boundries2.length; i++) {  
			String [] fields = PApplet.split(boundries2[i], ',');
			float x = Float.valueOf(fields[0]);
			float y = Float.valueOf(fields[1]);
			float z = Float.valueOf(fields[2]);
			Vec3D v = new Vec3D (x, y, z);
			p5.alisBnd2.add(v);
		}
		boundries3 =p5.loadStrings("data/"+p5.SiteFolder+"/BND3.txt"); 
		for (int i= 0; i <boundries3.length; i++) {  
			String [] fields = PApplet.split(boundries3[i], ',');
			float x = Float.valueOf(fields[0]);
			float y = Float.valueOf(fields[1]);
			float z = Float.valueOf(fields[2]);
			Vec3D v = new Vec3D (x, y, z);
			p5.alisBnd3.add(v);
		}	
		boundries4 =p5.loadStrings("data/"+p5.SiteFolder+"/BND4.txt"); 
		for (int i= 0; i <boundries4.length; i++) {  
			String [] fields = PApplet.split(boundries4[i], ',');
			float x = Float.valueOf(fields[0]);
			float y = Float.valueOf(fields[1]);
			float z = Float.valueOf(fields[2]);
			Vec3D v = new Vec3D (x, y, z);
			p5.alisBnd4.add(v);
		}
		boundries5 =p5.loadStrings("data/"+p5.SiteFolder+"/BND5.txt"); 
		for (int i= 0; i <boundries5.length; i++) {  
			String [] fields = PApplet.split(boundries5[i], ',');
			float x = Float.valueOf(fields[0]);
			float y = Float.valueOf(fields[1]);
			float z = Float.valueOf(fields[2]);
			Vec3D v = new Vec3D (x, y, z);
			p5.alisBnd5.add(v);
		}	
	}

	void buildBox(float x, float y, float z) {
		p5.noFill();
		//p5.stroke(235, 30);
		p5.stroke(226,223,199,80);
		//p5.stroke(255);
		p5.strokeWeight(1);
		p5.pushMatrix();
		//p5.translate(p5.width/2, p5.height/2, p5.height/6);
		p5.translate(p5.width/2, p5.height/2, z/2);
		p5.scale(x, y, z);
		p5.box(1);
		p5.popMatrix();
		
		p5.stroke(226,223,199,40);
	     p5.strokeWeight(1);
	 	p5.pushMatrix();
	 	for(int i=0; i <11; i++){
	
        p5.translate(0,0,10);
 		if(i==9 ||i==4){
		    p5.beginShape();
		    p5.vertex(0, 0);
		    p5.vertex(0,250);
		    p5.vertex(450, 250);
		    p5.vertex(450, 0);
		    p5.vertex(0, 0); 		
		    p5.endShape();
 		}
	 	}
		    p5.popMatrix();
	}
	
	

	
	
	
	public void ReadAttractionState(String sFileName)
	{
		try {

			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			Document doc = docBuilder.parse(new File(sFileName));

			// normalize text representation
			doc.getDocumentElement().normalize();

			Element oElm = (Element) doc.getElementsByTagName(
					"AttractionConnectionPriority").item(0);

			NodeList listOfAttraction = oElm.getElementsByTagName("Attractor");
			
			p5.AttractionDictionary.clear();
			p5.alisDst.clear();
			p5.alisOrigin1.clear();
			p5.alisOrigin.clear();

			for (int s = 0; s < listOfAttraction.getLength(); s++) {
				Element examineAttractorInfo = (Element) listOfAttraction
						.item(s);

				Vec3D maxPos = ChangCeilingColor003
						.ParseVec3D(examineAttractorInfo.getAttribute("MaxPos"));
				Vec3D minPos = ChangCeilingColor003
						.ParseVec3D(examineAttractorInfo.getAttribute("MinPos"));

				
				Vec3D intialePos = maxPos.add(minPos).scale(0.5f);
				intialePos.z  =0; 

				String ID = examineAttractorInfo.getAttribute("Id");
				String description = examineAttractorInfo
						.getAttribute("description");
				int AttractionRadious = Integer.parseInt(examineAttractorInfo
						.getAttribute("AttractionRadious"));

				int minRadios = Integer.parseInt(examineAttractorInfo
						.getAttribute("Min_R"));
				int maxRadios = Integer.parseInt(examineAttractorInfo
						.getAttribute("Max_R"));

				Vec3D rgb = ChangCeilingColor003
						.ParseVec3D(examineAttractorInfo
								.getAttribute("RGBColor"));

				Attraction examineAttractor = new Attraction(p5, ID,
						description, (int) rgb.x, (int) rgb.y, (int) rgb.z,
						AttractionRadious, minPos, maxPos, minRadios,
						maxRadios, intialePos);

				examineAttractor.SetLocation(intialePos);
				
				int nMaxConnection = Integer.parseInt(examineAttractorInfo
						.getAttribute("MaxConnection"));

				if (examineAttractorInfo.getAttribute("IsEntrance") != null) {
					examineAttractor.m_bIsEntrance = Boolean
							.parseBoolean(examineAttractorInfo
									.getAttribute("IsEntrance"));
				}

				if (examineAttractorInfo.getAttribute("mass_radio") != "") {
					int nMassRadios = Integer.parseInt(examineAttractorInfo
							.getAttribute("mass_radio"));
					examineAttractor.setMassRadios(AttractionRadious);
				}

				if (examineAttractorInfo.getAttribute("IsExit") != null) {
					examineAttractor.m_bIsExit = Boolean
							.parseBoolean(examineAttractorInfo
									.getAttribute("IsExit"));
				}

				if (examineAttractor.m_bIsEntrance
						|| examineAttractor.m_bIsExit) {
					p5.alisOrigin1.add(examineAttractor);
					p5.alisOrigin.add(examineAttractor);
				}

				NodeList transformerSettings = examineAttractorInfo
						.getElementsByTagName("Transformer");
				if (transformerSettings.getLength() > 0) {
					Element tranformationElm = (Element) transformerSettings
							.item(0);
					Vec3D transformationTarget = ChangCeilingColor003
							.ParseVec3D(tranformationElm.getAttribute("To"));
					examineAttractor.SetTransformation(transformationTarget);
				}

				examineAttractor.SetMaxNumberOfConnection(nMaxConnection);
				NodeList attractorPriority = examineAttractorInfo
						.getElementsByTagName("ConnectTo");

				for (int i = 0; i < attractorPriority.getLength(); ++i) {
					examineAttractor
							.AddAttractorToPreferdList(((Element) attractorPriority
									.item(i)).getAttribute("Id"));
				}

				p5.AttractionDictionary.put(ID, examineAttractor);
				p5.alisDst.add(examineAttractor);
			}

			NodeList listOfLimits = doc.getElementsByTagName("Limit");
			ArrayList<AttractionInteractionProperties> AttractionInteractionPropertiesHolder = new ArrayList<AttractionInteractionProperties>();

			int totalAttraction = listOfLimits.getLength();
			System.out.println("Total no of attraction : " + totalAttraction);

//			for (int s = 0; s < listOfLimits.getLength(); s++) {
//
//				Node firstAttractionNode = listOfLimits.item(s);
//				if (firstAttractionNode.getNodeType() == Node.ELEMENT_NODE) {
//
//					Element firstPersonElement = (Element) firstAttractionNode;
//					if (firstPersonElement.getAttribute("Attractor1") != null
//							&& firstPersonElement.getAttribute("Attractor2") != null) {
//						Element LimitMaxDistance = (Element) firstPersonElement
//								.getElementsByTagName("MaxDistance").item(0);
//						Element LimitMinDistance = (Element) firstPersonElement
//								.getElementsByTagName("MinDistance").item(0);
//						double max_distance = Double
//								.parseDouble(LimitMaxDistance
//										.getAttribute("Value"));
//						double min_distance = Double
//								.parseDouble(LimitMinDistance
//										.getAttribute("Value"));
//
//						AttractionInteractionProperties newAttractionProperties = new AttractionInteractionProperties(
//								firstPersonElement.getAttribute("Attractor1"),
//								firstPersonElement.getAttribute("Attractor2"),
//								min_distance, max_distance);
//						AttractionInteractionPropertiesHolder
//								.add(newAttractionProperties);
//					}
//				}
//			}

//			p5.AttractionInteractionPropertiesHolder = new AttractionInteractionHolder(
//					AttractionInteractionPropertiesHolder);

		} catch (SAXParseException err) {
			System.out.println("** Parsing error" + ", line "
					+ err.getLineNumber() + ", uri " + err.getSystemId());
			System.out.println(" " + err.getMessage());

		} catch (SAXException e) {
			Exception x = e.getException();
			((x == null) ? e : x).printStackTrace();

		} catch (Throwable t) {
			t.printStackTrace();
		}
	}

}
